#include "wrap_QLine.h"
#include "qluascriptvalue.h"
#include "qluascriptcontext.h"
#include "qluaexception.h"
#include <QDebug>

QLine_Prototype::QLine_Prototype(QObject * parent)
    : QObject(parent)
{
}

QLine * QLine_Prototype::ptr() const
{
    QLine * ptr = qtlua_scriptvalue_cast<QLine>(thisObject());
    if(!ptr) {
        engine()->throwException(engine()->newException(QLatin1String("QLine_prototype: Unable to cast this object to QLine")));
    }
    return ptr;
}

QLineF * QLineF_Prototype::ptr() const
{
    QLineF * ptr = qtlua_scriptvalue_cast<QLineF>(thisObject());
    if(!ptr) {
        engine()->throwException(engine()->newException(QLatin1String("QLineF_prototype: Unable to cast this object to QLineF")));
    }
    return ptr;
}

QLineF_Prototype::QLineF_Prototype(QObject * parent)
    : QObject(parent)
{
}

QLine_wrapper::QLine_wrapper(QLuaScriptEngine * engine)
    : QObject(engine),
      QLuaScriptClass(engine)
{
    m_proto = engine->newQObject(new QLine_Prototype(engine),
                                 QLuaScriptEngine::ScriptOwnership);

    engine->setGlobal("QLine", engine->newQObject(this));
    engine->setGlobal("QLine_prototype", m_proto);

    qluaRegisterScriptMetaType<QLine>(engine,
                                      QLine_toScriptValue,
                                      QLine_fromScriptValue);
}

QLine_wrapper::QueryFlags QLine_wrapper::queryProperty(const QLuaScriptValue& object,
                                 const QByteArray& name)
{
    if(name == "newLine") {
        return HandlesCallAccess;
    }

    return 0;
}

QLuaScriptValue QLine_wrapper::property(const QLuaScriptValue& object,
                                      const QByteArray& name)
{
    if(name == "newLine") {
        return scriptEngine()->newFunction(new_line);
    }

    return QLuaScriptValue();
}

QLuaScriptValue QLine_wrapper::prototype() const
{
    return m_proto;
}

QString QLine_wrapper::name() const
{
    return QLatin1String("QLine");
}

QLuaScriptValue QLine_wrapper::QLine_toScriptValue(QLuaScriptEngine * engine, const QLine * t)
{
    QLuaScriptValue value = engine->newUserDataWithGc<QLine>(engine->getGlobal("QLine_prototype"));
    QLine * obj = qtlua_scriptvalue_cast<QLine>(value);
    if(t)
        *obj = *t;
    return value;
}

void QLine_wrapper::QLine_fromScriptValue(const QLuaScriptValue& value, QLine * t)
{
    QLine * scriptT = qtlua_scriptvalue_cast<QLine>(value);
    if(scriptT) {
        *t = *scriptT;
    } else {
        qWarning("[QtLua] Trying to convert from another metatype of userdata to QLine");
    }
}

QLuaScriptValue QLine_wrapper::new_line(QLuaScriptContext * context, QLuaScriptEngine * engine)
{
    QLuaScriptValue value = engine->newUserDataWithGc<QLine>(engine->getGlobal("QLine_prototype"));
    QLine * obj = qtlua_scriptvalue_cast<QLine>(value);
    if(context->argumentCount() == 1)
    {
        if(QLine * p2 = qtlua_scriptvalue_cast<QLine>(context->argument(0))) {
            *obj = *p2;
        } else if(context->argument(0).isTable()) {
            obj->setLine(context->argument(0).property("x1").toInteger(),
                         context->argument(0).property("y1").toInteger(),
                         context->argument(0).property("x2").toInteger(),
                         context->argument(0).property("y2").toInteger());
        }
    } else if(context->argumentCount() == 4) {
        obj->setLine(context->argument(0).toInteger(),
                     context->argument(1).toInteger(),
                     context->argument(2).toInteger(),
                     context->argument(3).toInteger());
    } else if(context->argumentCount() == 2) {
        if(QPoint * p1 = qtlua_scriptvalue_cast<QPoint>(context->argument(0))) {
            if(QPoint * p2 = qtlua_scriptvalue_cast<QPoint>(context->argument(1))) {
                obj->setPoints(*p1, *p2);
            }
        }
    }

    return value;
}

QLineF_wrapper::QLineF_wrapper(QLuaScriptEngine * engine)
    : QObject(engine),
      QLuaScriptClass(engine)
{
    m_proto = engine->newQObject(new QLineF_Prototype(engine),
                                 QLuaScriptEngine::ScriptOwnership);

    engine->setGlobal("QLineF", engine->newQObject(this));
    engine->setGlobal("QLineF_prototype", m_proto);

    qluaRegisterScriptMetaType<QLineF>(engine,
                                       QLineF_toScriptValue,
                                       QLineF_fromScriptValue);
}

QLineF_wrapper::QueryFlags QLineF_wrapper::queryProperty(const QLuaScriptValue& object,
                                 const QByteArray& name)
{
    if(name == "newLine") {
        return HandlesCallAccess;
    }

    return 0;
}

QLuaScriptValue QLineF_wrapper::property(const QLuaScriptValue& object,
                                      const QByteArray& name)
{
    if(name == "newLine") {
        return scriptEngine()->newFunction(new_lineF);
    }

    return QLuaScriptValue();
}

QLuaScriptValue QLineF_wrapper::prototype() const
{
    return m_proto;
}

QString QLineF_wrapper::name() const
{
    return QLatin1String("QLineF");
}

QLuaScriptValue QLineF_wrapper::QLineF_toScriptValue(QLuaScriptEngine * engine, const QLineF * t)
{
    QLuaScriptValue value = engine->newUserDataWithGc<QLineF>(engine->getGlobal("QLineF_prototype"));
    QLineF * obj = qtlua_scriptvalue_cast<QLineF>(value);
    if(t)
        *obj = *t;
#ifdef QT_DEBUG
    qDebug() << "Create QLineF" << obj->p1() << obj->p2();
#endif
    return value;
}

void QLineF_wrapper::QLineF_fromScriptValue(const QLuaScriptValue& value, QLineF * t)
{
    QLineF * scriptT = qtlua_scriptvalue_cast<QLineF>(value);
    if(scriptT) {
        *t = *scriptT;
    } else {
        qWarning("[QtLua] Trying to convert from another metatype of userdata to QLineF");
    }
}

QLuaScriptValue QLineF_wrapper::new_lineF(QLuaScriptContext * context, QLuaScriptEngine * engine)
{
    QLuaScriptValue value = engine->newUserDataWithGc<QLineF>(engine->getGlobal("QLineF_prototype"));
    QLineF * obj = qtlua_scriptvalue_cast<QLineF>(value);

    if(context->argumentCount() == 1)
    {
        if(QLineF * p2 = qtlua_scriptvalue_cast<QLineF>(context->argument(0))) {
            *obj = *p2;
        } else if(context->argument(0).isTable()) {
            obj->setLine(context->argument(0).property("x1").toNumber(),
                         context->argument(0).property("y1").toNumber(),
                         context->argument(0).property("x2").toNumber(),
                         context->argument(0).property("y2").toNumber());
        }
    } else if(context->argumentCount() == 4) {
        obj->setLine(context->argument(0).toNumber(),
                     context->argument(1).toNumber(),
                     context->argument(2).toNumber(),
                     context->argument(3).toNumber());
    } else if(context->argumentCount() == 2) {
        if(QPointF * p1 = qtlua_scriptvalue_cast<QPointF>(context->argument(0))) {
            if(QPointF * p2 = qtlua_scriptvalue_cast<QPointF>(context->argument(1))) {
                obj->setPoints(*p1, *p2);
            }
        }
    }

    return value;
}
